package com.cn.xcsharp.controller;

import com.cn.xcsharp.bean.PagingBean;
import com.cn.xcsharp.service.IPublicMysqlDataService;
import com.cn.xcsharp.util.CommonResult;
import com.cn.xcsharp.util.CommonResultFy;
import com.cn.xcsharp.util.JwtUtil;
import com.cn.xcsharp.util.StrUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/mysql/v1")
public class MysqlController {

    @Resource
    private IPublicMysqlDataService Ipmds;
    @RequestMapping("/add")
    public CommonResult add(String itemName,String itemDetail) {
        CommonResult result = new CommonResult();
        try {
            String sql="insert into s_item(itemname,itemdetail)values('"+itemName+"','"+itemDetail+"')";
            System.out.println("sql:"+sql);
            int num=Ipmds.insertDB(sql);
            //将查询结果封装到CommonResult中
            result.setData(num);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(501);
            result.setMsg(StrUtils.INFO_501);
            return result;
        }
    }

    @RequestMapping("/update")
    public CommonResult update(String id,String itemName,String itemDetail) {
        CommonResult result = new CommonResult();
        try {
            String sql="update s_item set itemname='"+itemName+"',itemdetail='"+itemDetail+"' where id="+id;
            System.out.println("sql:"+sql);
            int num=Ipmds.updateDB(sql);
            //将查询结果封装到CommonResult中
            result.setData(num);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(501);
            result.setMsg(StrUtils.INFO_501);
            return result;
        }
    }
    @RequestMapping("/del")
    public CommonResult del(String id) {
        CommonResult result = new CommonResult();
        try {
            String sql="delete from s_item where id="+id;
            System.out.println("sql:"+sql);
            int num=Ipmds.deleteDB(sql);
            //将查询结果封装到CommonResult中
            result.setData(num);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(501);
            result.setMsg(StrUtils.INFO_501);
            return result;
        }
    }
    @RequestMapping("/getlist")
    public CommonResult getlist() {
        CommonResult result = new CommonResult();
        try {
            String sql="select * from s_item";
            System.out.println("sql:"+sql);
            List list=Ipmds.getList(sql,new  Object[]{});
            //将查询结果封装到CommonResult中
            result.setData(list);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(501);
            result.setMsg(StrUtils.INFO_501);
            return result;
        }
    }

    @RequestMapping("/getfylist")
    public CommonResultFy getfylist(int limit, int page, String itemName, HttpServletRequest request){
        CommonResultFy result = new CommonResultFy();
        try {
            String username = JwtUtil.getUserNameByToken(request);
            //分页
            if(!itemName.equals("")&&!itemName.equals(null)){
                itemName=" and itemName like '%"+itemName+"%' ";
            }
            String sql="select * from S_ITEM t where 1 = 1"+itemName;
            PagingBean pagingBean = Ipmds.getListFy(sql,limit,page,new  Object[]{});
            //将查询结果封装到CommonResult中
            result.setCount(pagingBean.getTotalCount());
            result.setData(pagingBean.getData());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setState(501);
            result.setMsg(StrUtils.INFO_501);
            return result;
        }
    }
}
